package ar.com.jwa.services;

import java.util.Collection;
import java.util.Map;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import ar.com.jwa.domain.Game;
import ar.com.jwa.domain.State;
import ar.com.jwa.domain.User;

public class SystemFacade {
	
	private UserLocalService userLocalService;
	
	private GameLocalService gameLocalService;
	
	private Map<String,String> emailingConfiguration;


	public SystemFacade() {
		super();
	}

	public void saveUser(User user){
		this.getUserLocalService().saveUser(user);
	}


	public User getUserByMail(String email) throws Exception{
		return this.getUserLocalService().getUserByMail(email);
	}
	
	
	public User login(User user) {
		return this.userLocalService.login(user);
	}
	
	@Transactional
	public void addPlayerToGameConfirmedList(String gameId,Long userId){
		
		Game game = this.getGameLocalService().getGameById(gameId);
		User player = this.getUserLocalService().getUserById(userId);
		
		if (game != null && player != null){
			
			if (game.hasRefused(player)){
				game.removeFromRefusedPlayers(player);
			}
			game.confirmPlayer(player);
		}
		else{
			throw new NullPointerException("The user id or the Game id are in and invalid state");
		}
	
	}
	
	@Transactional
	public void addPlayerToGameNotGoingList(String gameId, Long userId) {
		
		Game game = this.getGameLocalService().getGameById(gameId);
		
		User player = this.getUserLocalService().getUserById(userId);
		
		if (game != null && player != null){
			
			if (game.getConfirmedPlayers().contains(player)){
				game.getConfirmedPlayers().remove(player);
			}
			
			game.getRefusedPlayers().add(player);
		
		}
		else{
			throw new NullPointerException("The user id or the Game id are in and invalid state");
		}
		
	}
	
	public Collection<Game> getGamesToConfirm(User user){
		return this.getGameLocalService().getGamesByState(State.TO_CONFIRM, user);
	}
	
	@Transactional(propagation = Propagation.REQUIRED)
	public Collection<User> getConfirmedPlayersForGame(String gameId){
		Game game = this.getGameLocalService().getGameById(gameId);
		return game.getConfirmedPlayers();
	}
	
	
	/**
	 * @return the gameLocalService
	 */
	public GameLocalService getGameLocalService() {
		return gameLocalService;
	}

	/**
	 * @param gameLocalService the gameLocalService to set
	 */
	public void setGameLocalService(GameLocalService gameLocalService) {
		this.gameLocalService = gameLocalService;
	}

	/**
	 * @return the userLocalService
	 */
	public UserLocalService getUserLocalService() {
		if (this.userLocalService == null){
			throw new RuntimeException("userLocalService is not setted up");
		}
		return userLocalService;
	}
	
	/**
	 * @param userLocalService the userLocalService to set
	 */
	public void setUserLocalService(UserLocalService userLocalService) {
		this.userLocalService = userLocalService;
	}

	/**
	 * @return the emailingConfiguration
	 */
	public Map<String, String> getEmailingConfiguration() {
		return emailingConfiguration;
	}

	/**
	 * @param emailingConfiguration the emailingConfiguration to set
	 */
	public void setEmailingConfiguration(Map<String, String> emailingConfiguration) {
		this.emailingConfiguration = emailingConfiguration;
	}

	
}
